<?php

/**
 * An object set contains any number of objects. Adding to the set an object that is already contained in it does not modify the set.
 * 
 * An object set can contain non-object values, but they will be boxed internally and might make some methods, such as contains(), non-near-constant-time. It also requires PHP 5.2.0 or later.
 */
class CObjectSet implements ICSet {
	private $set = array();
	
	function add($value) {
		$value = CBox($value);
		$this->set[spl_object_hash($value)] = $value;
		return $this;
	}
	
	function remove($value) {
		if (isset($this->set[spl_object_hash($value)]))
			unset($this->set[spl_object_hash($value)]);
		return $this;
	}
	
	function contains($value) {
		if (is_object($value))
			return isset($this->set[spl_object_hash($value)]);
		else {
			foreach ($this->set as $id => $object) {
				if (!($object instanceof ICUnboxable)) continue;
				if ($value == $object->unboxedValue())
					return true;
			}
			
			return false;
		}
	}
	
	function allItems() {
		$a = array_values($this->set);
		$l = sizeof($a);
		for ($i = 0; $i < $l; $i++)
			$a[$i] = CUnbox($a[$i]);
		return $a;
	}
}

?>